<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class CreateOriginalPlanNodeDetailTable extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function up()
    {
        $table = $this->table('original_plan_node_detail', ['id' => false, 'primary_key' => ['id'], 'engine' => 'InnoDB', 'collation' => 'utf8mb4_general_ci', 'comment' => '原计划排期节点-详细表']);
        $table->addColumn('id', 'integer', ['identity' => true, 'signed' => false, 'limit' => 11, 'comment' => '主键ID'])
            ->addColumn('project_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '项目id'])
            ->addColumn('task_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '任务id'])
            ->addColumn('entity_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '实体ID'])
            ->addColumn('entity_module_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '实体所属模块id'])
            ->addColumn('initial_episode_code', 'string', ['default' => '', 'limit' => 128, 'comment' => '所属集数code'])
            ->addColumn('showings_number', 'string', ['default' => '', 'null' => false, 'limit' => 20, 'comment' => '所属场次'])
            ->addColumn('original_plan_node_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '原计划节点id'])
            ->addColumn('original_delivery_time', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '原计划交付节点交付时间'])
            ->addColumn('delivery_status', 'integer', ['limit' => 255, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '交付状态（0=默认，进行中 1=已交付 2=未交付）'])
            ->addColumn('step_category_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '环节ID'])

            ->addColumn('new_delivery_time', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '延期交付时间，产生新的节点时候填充新的交付时间'])
            ->addColumn('close_link_type', 'integer', ['limit' => 255, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '异常结束，来源类型（0=默认无，1=延期单、2=工单、3=需求单、4=排期、5、废除、6、启动、7、逾期交付）'])
            ->addColumn('close_link_id', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '异常结束，来源ID（0=默认无，延期单ID、工单ID、需求单ID、排期ID）'])

            ->addColumn('step_type', 'enum', [
                'values' => 'step,step_category,to_do,work_order_to_do,entity_to_do,standard_meeting,standard_review,standard_make_to_do,template_to_do,person',
                'default' => 'step',
                'comment' => '任务工序关联类型,step工序任务,step_category 工序分类任务,to_do 待办任务,work_order_to_do 工单待办任务,entity_to_do 实体待办,standard_meeting 标准会议待办,standard_review 标准审核,standard_make_to_do 标准制作待办,template_to_do 模板代办主任务,person 个人代办',
            ])
            ->addColumn('node_type', 'integer', ['limit' => 255, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '节点类型: 1=交付节点,2=完成节点'])
            ->addColumn('created', 'integer', ['limit' => 11, 'null' => false, 'signed' => false, 'default' => 0, 'comment' => '原计划交付节点创建时间'])

            ->addIndex(['original_plan_node_id', 'delivery_status'], ['type' => 'normal', 'name' => 'delivery_status'])
            ->addIndex(['task_id', 'delivery_status'], ['type' => 'normal', 'name' => 'task_delivery_status'])
            ->addIndex(['entity_id', 'delivery_status', 'step_category_id'], ['type' => 'normal', 'name' => 'entity_delivery_status'])
            ->addIndex(['original_delivery_time', 'project_id', 'delivery_status'], ['type' => 'normal', 'name' => 'project_delivery_time']);
        // 创建表
        $table->create();
    }

    public function down()
    {
        $this->table('original_plan_node_detail')->drop()->save();
    }
}
